<template>
  <div class="page-content">
    <el-card>
      <TableBar :layout="layout">
        <el-row slot="bottom" :gutter="20" type="flex">
          <el-col :span="7">
            <el-button
              type="primary"
              size="small"
              @click="createDialogVisible = true"
              >创建仓库</el-button
            >
          </el-col>
          <el-col :span="14">
            <el-select v-model="baseId" size="small" placeholder="请选择基地" @change="getTableData">
              <el-option
                v-for="item in baseList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-row>
      </TableBar>
      <div class="bigBox">
        <warehouse
          @fatherMethod="getTableData"
          :toWarehouse="item"
          v-for="item in tableData"
          :key="item.id"
        />
      </div>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.current"
        :page-sizes="[6, 12, 18, 24]"
        :page-size="page.sizea"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
      >
      </el-pagination>
    </el-card>
    <el-dialog title="创建仓库" width="27%" :visible.sync="createDialogVisible">
      <el-form :modle="createInfo" label-width="110px">
        <el-form-item label="仓库名称">
          <el-select v-model="createInfo.baseId" placeholder="请选择基地">
            <el-option
              v-for="item in baseList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="仓库名称">
          <el-input v-model="createInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="仓库面积/m²">
          <el-input-number
            v-model="createInfo.area"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="createDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="createWarehouseEvent"
          >确 定</el-button
        >
      </span></el-dialog
    >
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import {
  getWarehouseInfo,
  createWarehouse,
} from "../../Api/productionmanage/Supply_warehouse";
import warehouse from "../../components/ProductionManagement/warehouse/warehouse";
export default {
  components: {
    warehouse,
  },
  data() {
    return {
      msg: "仓库管理",
      tableData: [],
      page: {
        size: 6,
        current: 1,
        total: 0,
      },
      layout: " refresh",
      createDialogVisible: false,
      createInfo: {},
      baseId: null,
    };
  },
  computed: {
    ...mapState({
      baseList: (state) => state.productionInfo.baseInfoList,
    }),
  },
  created() {
    this.getTableData();
  },
  methods: {
    async getTableData() {
      if (!this.baseId) {
        this.baseId = this.baseList[0].id;
      }
      const { data: res } = await getWarehouseInfo(
        this.baseId,
        this.page.size,
        this.page.current
      );
      this.tableData = res.data.records;
      this.page.total = res.data.total;
      console.log(res);
    },
    handleSizeChange(val) {
      this.page.size = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.page.current = val;
      this.getTableData();
    },
    async createWarehouseEvent() {
      console.log(this.createInfo);
      const { data: res } = await createWarehouse(this.createInfo);
      console.log(res);
      if (res.statusCode === 20000) {
        this.$message.success("创建仓库成功");
        this.createDialogVisible = false;
        this.getTableData();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.bigBox {
  display: flex;
  flex-wrap: wrap;
  font-size: 14px;
}
.bigBox p {
  color: #636e7b;
  margin: 5px 0;
}
</style>